package com.weather.business.service.impl;

import com.weather.business.domain.SevenEchars;
import com.weather.business.domain.WeatherSevenData;
import com.weather.business.mapper.WeatherSevenDataMapper;
import com.weather.business.service.IWeatherSevenDataService;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 城市7日天气Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-19
 */
@Service
public class WeatherSevenDataServiceImpl implements IWeatherSevenDataService
{
    @Autowired
    private WeatherSevenDataMapper weatherSevenDataMapper;

    /**
     * 查询城市7日天气
     *
     * @param id 城市7日天气主键
     * @return 城市7日天气
     */
    @Override
    public WeatherSevenData selectWeatherSevenDataById(Long id)
    {
        return weatherSevenDataMapper.selectWeatherSevenDataById(id);
    }

    /**
     * 查询城市7日天气列表
     *
     * @param weatherSevenData 城市7日天气
     * @return 城市7日天气
     */
    @Override
    public List<WeatherSevenData> selectWeatherSevenDataList(WeatherSevenData weatherSevenData)
    {
        return weatherSevenDataMapper.selectWeatherSevenDataList(weatherSevenData);
    }

    /**
     * 查询城市7日天气列表
     *
     * @param weatherSevenData 城市7日天气
     * @return 城市7日天气
     */
    @Override
    public List<SevenEchars> ecahrsList(WeatherSevenData weatherSevenData)
    {
        List<WeatherSevenData> originalList  = weatherSevenDataMapper.selectWeatherSevenDataList(weatherSevenData);
        Map<String, List<WeatherSevenData>> cityForecastsMap = originalList.stream()
                .collect(Collectors.groupingBy(WeatherSevenData::getCityid));

        // 定义一个Map来存储每个城市的结果
        Map<String, Pair<List<String>, List<String>>> cityTemAndDates = new HashMap<>();
        List<SevenEchars> sevenEcharsList = new ArrayList<>();

        // 遍历每个城市的数据
        for (Map.Entry<String, List<WeatherSevenData>> entry : cityForecastsMap.entrySet()) {
            String cityId = entry.getKey();
            List<WeatherSevenData> cityForecasts = entry.getValue();

            SevenEchars echars  =  new SevenEchars();

            // 提取城市名称
            String cityName = cityForecasts.get(0).getCity();

            echars.setCity(cityName);


            for (WeatherSevenData forecast : cityForecasts) {
                // 添加白天最高气温
                echars.getTem().add(forecast.getTemDay());

                // 将日期转换为字符串，并添加到日期列表中
                echars.getDays().add(new SimpleDateFormat("yyyy-MM-dd").format(forecast.getDate()));
            }
            sevenEcharsList.add(echars);
        }

        return sevenEcharsList;
    }

    /**
     * 新增城市7日天气
     *
     * @param weatherSevenData 城市7日天气
     * @return 结果
     */
    @Override
    public int insertWeatherSevenData(WeatherSevenData weatherSevenData)
    {
        return weatherSevenDataMapper.insertWeatherSevenData(weatherSevenData);
    }

    /**
     * 修改城市7日天气
     *
     * @param weatherSevenData 城市7日天气
     * @return 结果
     */
    @Override
    public int updateWeatherSevenData(WeatherSevenData weatherSevenData)
    {
        return weatherSevenDataMapper.updateWeatherSevenData(weatherSevenData);
    }

    /**
     * 批量删除城市7日天气
     *
     * @param ids 需要删除的城市7日天气主键
     * @return 结果
     */
    @Override
    public int deleteWeatherSevenDataByIds(Long[] ids)
    {
        return weatherSevenDataMapper.deleteWeatherSevenDataByIds(ids);
    }

    /**
     * 删除城市7日天气信息
     *
     * @param id 城市7日天气主键
     * @return 结果
     */
    @Override
    public int deleteWeatherSevenDataById(Long id)
    {
        return weatherSevenDataMapper.deleteWeatherSevenDataById(id);
    }
}
